package com.netflexitysolutions.amazonws.s3.internal.operations;

import java.util.List;

import com.amazonaws.s3.doc._2006_03_01.AccessControlPolicy;
import com.amazonaws.s3.doc._2006_03_01.GetObjectAccessControlPolicy;
import com.netflexitysolutions.amazonws.s3.S3;
import com.netflexitysolutions.amazonws.s3.S3AccessControlPolicy;
import com.netflexitysolutions.amazonws.s3.S3Grant;
import com.netflexitysolutions.amazonws.s3.S3Owner;
import com.netflexitysolutions.amazonws.s3.internal.utils.S3Utils;

public class GetObjectAccessControlPolicyOperation extends OperationExecutor<GetObjectAccessControlPolicy, S3AccessControlPolicy>{

    private String bucket;
    private String key;

    public GetObjectAccessControlPolicyOperation(S3 s3, String bucket, String key){
        super(s3);
        this.bucket = bucket;
        this.key = key;
    }

    @Override
    protected S3AccessControlPolicy call(GetObjectAccessControlPolicy request) {
        request.setBucket(bucket);
        request.setKey(key);
        injectCredentials(request, "GetObjectAccessControlPolicy", getS3().getAccessKey(), getS3().getSecretKey());
        AccessControlPolicy acp = getS3().getService().getObjectAccessControlPolicy(request).getGetObjectAccessControlPolicyResponse();
        List<S3Grant> acl = S3Utils.convert(acp.getAccessControlList());
        S3Owner owner = new S3Owner(acp.getOwner().getID(), acp.getOwner().getDisplayName());
        return new S3AccessControlPolicy(owner, acl);
    }
}
